package com.ljc.config;

import com.ljc.constant.RabbitMqConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置
 */
@Slf4j
@Configuration
public class InterfaceRabbitMqConfig {
    // 手动触发ConnectionListener 便可解决通过@Bean创建队列不生效的问题
    @Bean
    ApplicationRunner runner(ConnectionFactory cf) {
        log.info("接口服务消息队列创建成功");
        return args -> cf.createConnection().close();
    }

    /**
     * 删除缓存的队列
     */
    @Bean
    public Queue interfaceDeleteQueue() {
        return new Queue(RabbitMqConstant.INTERFACE_DELETE_QUEUE_NAME, true, false, false);
    }

    /**
     * 更新缓存的队列
     */
    @Bean
    public Queue interfaceUpdateQueue() {
        return new Queue(RabbitMqConstant.INTERFACE_UPDATE_QUEUE_NAME, true, false, false);
    }

    /**
     * 接口信息交换机
     */
    @Bean
    public Exchange interfaceExchange() {
        return new DirectExchange(RabbitMqConstant.INTERFACE_EXCHANGE_NAME, true, false);
    }


    /**
     * 交换机和删除缓存队列绑定
     */
    @Bean
    public Binding interfaceDeleteBinding() {
        return new Binding(RabbitMqConstant.INTERFACE_DELETE_QUEUE_NAME, Binding.DestinationType.QUEUE,
                RabbitMqConstant.INTERFACE_EXCHANGE_NAME, RabbitMqConstant.INTERFACE_DELETE_EXCHANGE_ROUTING_KEY, null);
    }

    /**
     * 交换机和更新缓存队列绑定
     */
    @Bean
    public Binding interfaceUpdateBinding() {
        return new Binding(RabbitMqConstant.INTERFACE_UPDATE_QUEUE_NAME, Binding.DestinationType.QUEUE,
                RabbitMqConstant.INTERFACE_EXCHANGE_NAME, RabbitMqConstant.INTERFACE_UPDATE_EXCHANGE_ROUTING_KEY, null);
    }

    /**
     * 创建操作接口信息出错交换机
     */
    @Bean
    public DirectExchange interfaceErrorExchange() {
        return new DirectExchange(RabbitMqConstant.INTERFACE_ERROR_EXCHANGE_NAME, true, false);
    }

    /**
     * 创建操作接口信息出错队列
     */
    @Bean
    public Queue interfaceErrorQueue() {
        return new Queue(RabbitMqConstant.INTERFACE_ERROR_QUEUE_NAME, true, false, false);
    }

    /**
     * 绑定创建操作接口信息出错队列和交换机
     */
    @Bean
    public Binding interfaceErrorBinding() {
        return BindingBuilder.bind(interfaceErrorQueue()).to(interfaceErrorExchange()).with(RabbitMqConstant.INTERFACE_ERROR_EXCHANGE_ROUTING_KEY);
    }

    /**
     * 重试耗尽后，将失败消息投递到出错交换机
     */
    @Bean
    public MessageRecoverer republishMessageRecoverer(RabbitTemplate rabbitTemplate) {
        return new RepublishMessageRecoverer(rabbitTemplate, RabbitMqConstant.INTERFACE_ERROR_EXCHANGE_NAME, RabbitMqConstant.INTERFACE_ERROR_EXCHANGE_ROUTING_KEY);
    }

}
